Tags: recurrence relations
State (but do not solve) the recurrence describing the run time of the following code, assuming that the input, arr
, is a list of size
def foo(arr, stop=None):
if stop is None:
stop = len(arr) - 1
if stop < 0:
return 1
last = arr[stop]
return last * foo(arr, stop - 1)
Tags: recurrence relations
What is the solution of the below recurrence? State your answer using asymptotic notation as a function of
Tags: recurrence relations
State (but do not solve) the recurrence describing the runtime of the following function.
def foo(n):
if n < 10:
print("Hello world.")
return
for i in range(n):
print(i)
for i in range(6):
foo(n//3)
Tags: recurrence relations
Solve the below recurrence, stating the solution in asymptotic notation. Show your work.
We'll unroll several times:
The pattern seems to be that on the
The base case is reached when
If you reached this point, you got most of the credit. If you're unsure of what to do with
The first (and easiest) way is to realize that
The second approach is to remember log properties to simplify
In this case:
And therefore
Tags: recurrence relations
State (but do not solve) the recurrence describing the runtime of the following function.
def foo(n):
if n < 1:
return 0
for i in range(n**2):
print("here")
foo(n/2)
Tags: recurrence relations, mergesort
Consider the modification of mergesort
shown below, where one of the recursive calls has been replaced by an in-place version of selection_sort
. Recall that selection_sort
takes
def kinda_mergesort(arr):
"""Sort array in-place."""
if len(arr) > 1:
middle = math.floor(len(arr) / 2)
left = arr[:middle]
right = arr[middle:]
mergesort(left)
selection_sort(right)
merge(left, right, arr)
What is the time complexity of kinda_mergesort
?
Tags: recurrence relations
Solve the below recurrence, stating the solution in asymptotic notation. Show your work.
Unrolling several times, we find:
On the
It will take
Tags: recurrence relations
Solve the recurrence below, stating the solution in asymptotic notation. Show your work.
Tags: recurrence relations
State (but do not solve) the recurrence describing the run time of the following code, assuming that the input, arr
, is a list of size
def foo(arr, stop=None):
if stop is None:
stop = len(arr) - 1
if stop < 0:
return 1
last = arr[stop]
return last * foo(arr, stop - 1)